package com.yuanfeng.biz.front.user.web;


import com.yuanfeng.entity.Users;
import com.yuanfeng.util.JdbcUtil;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

public class ValidateUser extends HttpServlet {
	
	private Users user;
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
				//得到ajax里面穿来的参数
				String username = request.getParameter("name");
				
				try {
					//得到数据源
					QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
					//查询数据库是否有对应的用户资料,把查询到的第一行封装到Bean里面
					     user = (Users)qr.query("select * from users where name=?",
					    	new BeanHandler(Users.class)	 
							,new Object[]{username}		
					);
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException();
				}
				//发送到ajax页面的内容
			   if(user != null && user.getName().equals(username)){
					response.getWriter().write("yes");
				}else{
					response.getWriter().write("no");
				}
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	     	doGet(request, response);
		
	}

}
